package xj.toolkit.netty.handler.codec.http.response;

import org.apache.commons.lang3.StringUtils;

import com.google.common.base.Preconditions;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;

/**
 * @author hp
 */
public class ConstantResponse {
	private ConstantResponse() {
	}

	static final DefaultFullHttpResponse RESPONSE_200_OK;

	static final DefaultFullHttpResponse RESPONSE_400_NOBODY;

	static final DefaultFullHttpResponse RESPONSE_SERVER_BUSY;

	static {
		RESPONSE_200_OK = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
		RESPONSE_200_OK.content().writeBytes(Unpooled.wrappedBuffer("OK".getBytes()));
		RESPONSE_200_OK.headers().set(HttpHeaders.Names.CONTENT_LENGTH, "OK".getBytes().length);

		RESPONSE_400_NOBODY = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);

		RESPONSE_SERVER_BUSY = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
				HttpResponseStatus.SERVICE_UNAVAILABLE);
		RESPONSE_SERVER_BUSY.content().writeBytes(Unpooled.wrappedBuffer("Server Too Busy".getBytes()));
		RESPONSE_SERVER_BUSY.headers().set(HttpHeaders.Names.CONTENT_LENGTH, "Server Too Busy".getBytes().length);
	}

	public static FullHttpResponse getOkResponse() {
		return RESPONSE_200_OK.copy();
	}

	public static FullHttpResponse get400NobodyResponse() {
		return RESPONSE_400_NOBODY.copy();
	}

	public static FullHttpResponse getResponseServerBusy() {
		return RESPONSE_SERVER_BUSY.copy();
	}

	public static FullHttpResponse get200WithContentTypeResponse(String contentType) {
		DefaultFullHttpResponse response200WithContentType = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
				HttpResponseStatus.OK);
		response200WithContentType.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
		return response200WithContentType;
	}

	/**
	 * 使用指定的code以及codedesc构建一个http response。
	 * 
	 * @param httpCode
	 * @param desc
	 * @param contentType
	 * @return
	 */
	public static FullHttpResponse getWithCodeDesc(int httpCode, String desc, String contentType, String message) {
		HttpResponseStatus status = new HttpResponseStatus(httpCode, desc);

		return getWithCodeDesc(status, contentType, message);
	}

	/**
	 * 使用指定的code以及codedesc构建一个http response。
	 * 
	 * @param httpCode
	 * @param desc
	 * @param contentType
	 * @return
	 */
	public static FullHttpResponse getWithCodeDesc(HttpResponseStatus status, String contentType, String message) {
		DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
		response.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
		if (StringUtils.isEmpty(message)) {
			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(status.reasonPhrase().length()));
			response.content().writeBytes(status.reasonPhrase().getBytes());
		} else {
			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(message.length()));
			response.content().writeBytes(message.getBytes());
		}
		return response;
	}
}
